package com.panfeng.xcloud.scheduler.service.impl;

import com.panfeng.xcloud.common.core.enums.CoinListStatusTypeEnum;
import com.panfeng.xcloud.common.core.enums.PositionSideTypeEnum;
import com.panfeng.xcloud.common.core.utils.StringUtils;
import com.panfeng.xcloud.dao.assets.entity.CoinList;
import com.panfeng.xcloud.scheduler.dao.mapper.CoinListMapperExt;
import com.panfeng.xcloud.scheduler.dao.mapper.UserBotMapperExt;
import com.panfeng.xcloud.scheduler.service.IFeignService;
import com.panfeng.xcloud.scheduler.service.IMarketPriceService;
import com.panfeng.xcloud.scheduler.service.IRiskManagerService;
import groovy.util.logging.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
@Service
public class MarketPriceServiceImpl implements IMarketPriceService {

    private static Logger log = LoggerFactory.getLogger(MarketPriceServiceImpl.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CoinListMapperExt coinListMapperExt;

    @Autowired
    private UserBotMapperExt userBotMapperExt;

    @Autowired
    @Qualifier(value = "notLoadBalancedRestTemplate")
    private RestTemplate restTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IFeignService feignService;

    @Autowired
    private IRiskManagerService iTradingIndicatorsService;

    @Override
    public void getMarketPrice() {
        //获取全部交易所全部币种
        List<CoinList> coinLists = coinListMapperExt.selectByCondition(null, CoinListStatusTypeEnum.OPEN.code, PositionSideTypeEnum.BUY.getCode());

        //订阅盘口并更新价格
        coinLists.forEach(coin -> {
            try {
                //睡一下防止超频访问
                Thread.sleep(250);

                feignService.getAppearMarketPrice(coin.getExchangeType(), null, coin.getDestinationCoin(), coin.getSourceCoin());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                feignService.sendEmail("获取展示价格定时任务异常，交易对:" + coin.getDestinationCoin() + coin.getSourceCoin() +
                        "   异常信息:" + StringUtils.printStackTraceToString(e));
            }
        });
    }

}
